package com.time.toupdate.utils;

import com.time.toupdate.Job.InsertJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Author:B
 * Description:
 */
public class InsertRunner extends Thread{

    @Override
    public void run() {
        try {
            // 1、创建Scheduler（调度器）
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("everymonth", "myInsert") // 触发器名称和分组自定义
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ? ")) // 定时：此处cron表达式每2s执行一次
                    .build();
            TriggerKey key = trigger.getKey();

            JobDetail job = JobBuilder.newJob(InsertJob.class)
                    .withIdentity(key.getName(), key.getGroup())
                    .build();
            //4、将Job和Trigger交给Scheduler调度
            scheduler.scheduleJob(job, trigger);
            // 5、启动Scheduler
            scheduler.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}